package com.example.springboot.file.service.impl;

import com.example.springboot.file.entity.FileDatalVo;
import com.example.springboot.file.service.FtpClientService;
import com.example.springboot.file.util.FTPHelper;
import com.example.springboot.file.util.FileUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Service
public class FtpClientServiceImpl implements FtpClientService {
    private static final Log log = LogFactory.getLog(FtpClientServiceImpl.class);

    /**
     * @param files
     * @param systemType：系统类型7-其他
     * @return
     */
    @Override
    public List<FileDatalVo> saveAttachements(MultipartFile[] files, String systemType) {

        List<FileDatalVo> list = new ArrayList<FileDatalVo>();
        if (null == files || files.length <= 0)
            return list;

        //文件真实路径
        String remotePath = File.separator;
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            if (StringUtils.isNotBlank(fileName)) {
                String subfix = FileUtil.getFileSubfix(fileName);
                String fileName2 = "";// UUIDUtils.newGUID();
                try {
                    FTPHelper.createDir("/", remotePath.replace("/", ";"));
                    FTPHelper.uploadFile(remotePath, fileName2, file.getInputStream());
                    log.debug("文件 [" + fileName2 + "] 已上传至 ftp: [" + remotePath + "] 文件夹中.");

                    FileDatalVo fileDetailVo = new FileDatalVo();
                    fileDetailVo.setFileType(subfix);
                    fileDetailVo.setFilePath(remotePath);
                    fileDetailVo.setFileName(fileName2);
                    fileDetailVo.setMsg("上传成功");
                    fileDetailVo.setStatus(true);
                    list.add(fileDetailVo);

                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        }
        return list;
    }
}
